

/*
 * Engine
 */

var engine = new Engine();

//var build = engine.getCurrentBuild();

/*
 * Tests
 */

/* build.ClassId = 0;
build.Hp = 14995;
build.Defense = 2524;
build.Block = 50;
build.BlockAmount = 70; */

/* engine.addBuild();
engine.addBuild(); */

/*
 * 
 */

function initUIEventsFor(eventName) {
	$('body').on(eventName, 'input.inputHP', function(event) {
		build.Hp = $(this).val();
	});
	
	$('body').on(eventName, 'input.inputDefense', function(event) {
		build.Defense = $(this).val();
	});
	
	$('body').on(eventName, 'input.inputBlockScore', function(event) {
		build.Block = $(this).val();
	});
	
	$('body').on(eventName, 'input.inputBlockAmountScore', function(event) {
		build.BlockAmount = $(this).val();
	});
	
	$('body').on(eventName, 'input.inputDodgeScore', function(event) {
		build.Dodge = $(this).val();
	});
	
	$('body').on(eventName, 'input.inputParryScore', function(event) {
		build.Parry = $(this).val();
	});
}

initUIEventsFor('keyup');
//initUIEventsFor('change');

$('body').on('click', '.builds-list a', function(event) {
	if ($(this)[0].dataset.buildindex) {
		var buildIndex = parseInt($(this)[0].dataset.buildindex);
		
		engine.setCurrentBuild(buildIndex);
		$('.builds-list li.active').removeClass('active');
		$(this).parent().addClass('active');
	} else {
		var name = window.prompt('Enter new build name :');
		var newBuild = engine.addBuild();
		newBuild.Name = name;
	}
});

$('body').on('click', '.build-control .btn-rename', function(event) {
	var newName = window.prompt('Enter new build name :');
	build.Name = newName;
});

$('body').on('click', '.build-control .btn-delete', function(event) {
	if (window.confirm('Are you sure that you want to delete the current build ?')) {
		$('.builds-list li.active').remove();
		engine.deleteBuild(engine.currentBuildIndex);
	}
});

engine.on('currentbuildchanged', function(index) {
	build = engine.getCurrentBuild();
	$('input.inputHP').val(build.Hp);
	$('input.inputDefense').val(build.Defense);
	$('input.inputBlockScore').val(build.Block);
	$('input.inputBlockAmountScore').val(build.BlockAmount);
	$('input.inputParryScore').val(build.Parry);
	$('input.inputDodgeScore').val(build.Dodge);
	//if (index) {
		console.log(index);
		$('.builds-list li.active').removeClass('active');
		$('.builds-list li:nth-child(' + (index + 1) + ')').addClass('active');
	//}
	$('td.baseEH').text(Math.round(build.data.eh.base));
	$('td.baseEHDPB').text(Math.round(build.data.ehDPB.base));
});

engine.on('buildadded', function(newBuild, index) {
	$('.builds-list li.active').removeClass('active');
	$('.builds-list li:nth-last-child(1)').before('<li class="active"><a href="#" data-buildindex="' + index + '">' + newBuild.Name + '</a></li>')

	engine.setCurrentBuild(index);

	newBuild.on('namechanged', function() {
		$('.builds-list li:nth-child(' + (index + 1) + ') a').text(newBuild.Name);
	});

	newBuild.on('ehchanged', function() {
		$('td.baseEH').text(Math.round(newBuild.data.eh.base));
	});
	
	newBuild.on('ehdpbchanged', function() {
		$('td.baseEHDPB').text(Math.round(newBuild.data.ehDPB.base));
	});

});

engine.init();

var build = engine.getCurrentBuild();
